import {getAllVideos,getEvaluate,addHistory,getVideoHistory,updVideoHistory} from "../../api/api.js"
import {err,showToast} from "../../utils/util.js"
// pages/kecheng/kecheng.js
const openId = wx.getStorageSync('openid')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    touchS : [0,0],
    touchE : [0,0],
    //保存课程
    kc:{},
    //保存筛选之后的视频
    Allvodes:[],
    //保存哪个视频下标
    index:0,
    //z控制tab切换
    isShow:1,
    //保存评价
    Evaluate:[],
    //视频时长
    shichang:0,

    dianzan:false,
    skip: 0,
    page:0,
    total: 0
  },

  // 点赞
  dianzan() {
    this.setData({
      dianzan:!this.data.dianzan
    })
    if(this.data.dianzan){
      showToast('点赞成功','success',1500)
    } else {
      showToast('取消点赞','none',1000)
    }
  },

  
  //视频出错时触发
  ell(){
   setTimeout(()=>{
    wx.showToast({
      title: '视频加载失败，请稍后重试或观看其它视频',
      icon:'none'
    })
   },3000)
  },

  //获取组件列表传过来的参数
  updsp(e){
    console.log(this.data.Allvodes[e.detail]);
      this.setData({
        index:e.detail
      })
      this.addHistory()
    
  },

  //获取组件评论传过来的参数
  getpl(e){
    if (this.data.Evaluate.length >= this.data.total) {
      wx.showToast({
        title: '数组加载完毕',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '努力加载中',
    })
    this.setData({
      page:e.detail
    })
    this.data.skip = this.data.page * 10
    this.getEvaluate()
  },

  //添加观看历史
  addHistory(){
    
   try {
    const kcid = this.data.Allvodes[this.data.index].course_id._id
    const vid = this.data.Allvodes[this.data.index]._id
    getVideoHistory(openId,vid).then(res=>{
      console.log(res);
      if(res.data.data.length ==1){
        const id = res.data.data[0]._id
        updVideoHistory(id,kcid,vid).then(res=>{
          console.log(res);
        })
      }else{
        addHistory(openId,kcid,vid).then(res=>{
          console.log(res);
        })
      }
    })
   } catch (error) {
    err('当前网络不佳或没有视频,请稍后再试','../../pages/index/index')
   }
   
  },

  
  //点击评价去评价界面
  gopj(){
    if(!wx.getStorageSync('userInfo')){
      wx.showToast({
        title: '请先登录',
        icon:'none'
      })
      return
    }
    wx.setStorageSync('video', this.data.Allvodes[this.data.index])
    wx.navigateTo({
      url: '../../pages/pingjia/pingjia',
    })
  },

  //点击下载去资源页面
  xiazai(){
    wx.switchTab({
      url: '../../pages/ziyuan/ziyuan',
    })
  },

   //点击详情函数
   xq(){
    this.setData({
      isShow:2
    })
  },
  // 点击列表函数
  lb(){
    this.setData({
      isShow:1
    })
  },
  // 点击评论函数
  pj(){
    this.setData({
      isShow:3
    })
    this.getEvaluate()
  },

  //视频播放到底播放下一个
  enden(){
    if(this.data.index >= this.data.Allvodes.length-1){
      wx.showToast({
        title: '最后一个视频哦',
        icon:'none'
      })
      return
    }else{
    
        this.setData({
          index:this.data.index+1,
          // 修改收藏状态
          isSc:false
        })
        this.addHistory()
      
    }
    
    
  },

  // //记录当前时间时长
  jilu(e){
    const obj = {
      kc:this.data.Allvodes[this.data.index].course_id._id,
      index:this.data.index,
      sc:e.detail.currentTime
    }
    wx.setStorageSync('current_video', obj)
  
  },

  // 获取评论接口
  getEvaluate(){
    const kcid = this.data.Allvodes[this.data.index].course_id._id
    console.log(kcid);
    getEvaluate(kcid,this.data.skip).then(res=>{
      console.log(res);
      this.setData({
        Evaluate:this.data.Evaluate.concat(res.data.data),
        total: res.data.total
      })
      wx.hideLoading()
    })
  },


  // //获取当前视频时长
  getTime(){
   try {
    const obj = wx.getStorageSync('current_video')
    if(obj.kc==this.data.Allvodes[this.data.index].course_id._id){
      this.setData({
        index:obj.index
      })
      this.videoELe = wx.createVideoContext("myvideo")   

      this.videoELe.seek(obj.sc)
      this.videoELe.play()  

    }else{
      this.setData({
        index:0
      })
    }
   } catch (error) {
    err('当前网络不佳或没有视频,请稍后再试','../../pages/index/index')
   }
    
  },
 


  //获取本地储存对象
  getStorage(){
     // 获取带过来的对象
     const kc = wx.getStorageSync('kc')
      console.log(kc);
     this.setData({
       kc:kc
     })
  },

  // 获取所有视频
  getAllVideos(){
    getAllVideos().then(res=>{
      console.log(res);
      const data = res.data.data
      const Allvideos = []
      for(let i = 0;i<data.length;i++){
        if(data[i].course_id){
          if(this.data.kc._id == data[i].course_id._id || this.data.kc.link == data[i].course_id._id){
            if(data[i].video_url.includes('cloud') ){
              var start = data[i].video_url.indexOf(".");
              var end = data[i].video_url.indexOf("/",start);
              data[i].video_url = "https://"+data[i].video_url.substring(start+1,end)+".tcb.qcloud.la"+data[i].video_url.substring(end);
            }
            Allvideos.push(data[i])
          }
        }
        
      }
      this.setData({
        Allvodes:Allvideos
      })
      this.getTime()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.getStorage()

   

    this.getAllVideos()
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 //分享微信好友
 onShareAppMessage:function(from){
  if(from.from === 'menu'){
    return {
      title:'科云视频库，大量IT学习资源等你来',
      path:'/pages/index/index'
    }
  }
},
touchStart: function(e){
  // console.log(e.touches[0].pageX)
  let sx = e.touches[0].pageX
  let sy = e.touches[0].pageY
  this.data.touchS = [sx,sy]
},
touchMove: function(e){
  let sx = e.touches[0].pageX;
  let sy = e.touches[0].pageY;
  this.data.touchE = [sx, sy]
},
touchEnd: function(e){
  let start = this.data.touchS
  let end = this.data.touchE
  console.log(start)
  console.log(end)
  if(start[0] < end[0] - 80){
    this.setData({
      isShow:--this.data.isShow
    })
    if(this.data.isShow<1)
    {
      this.setData({
        isShow:1
      })
    }
  }else if(start[0] > end[0] + 80){
    this.setData({
      isShow:++this.data.isShow
    })
    if(this.data.isShow>=3)
    {
      this.setData({
        isShow:3
      })
      this.getEvaluate()
    }
  }else{
    console.log('静止')
  }
},
//分享朋友圈
onShareTimeline:function(){
  return {
    title:'科云视频库，大量IT学习资源等你来',
    imageUrl:"https://7975-yueyun188-0gdwo4vh4afabc50-1306555544.tcb.qcloud.la/cloudbase-cms/upload/2022-03-09/mras7605qjl0klsizgkbvfga5nrpsian_.png",
    path:'/pages/index/index'
  }
}
})